import java.util.LinkedList;
/*
 * @lc app=leetcode.cn id=257 lang=java
 *
 * [257] 二叉树的所有路径
 *
 * https://leetcode-cn.com/problems/binary-tree-paths/description/
 *
 * algorithms
 * Easy (56.94%)
 * Total Accepted:    6K
 * Total Submissions: 10.6K
 * Testcase Example:  '[1,2,3,null,5]'
 *
 * 给定一个二叉树，返回所有从根节点到叶子节点的路径。
 * 
 * 说明: 叶子节点是指没有子节点的节点。
 * 
 * 示例:
 * 
 * 输入:
 * 
 * ⁠  1
 * ⁠/   \
 * 2     3
 * ⁠\
 * ⁠ 5
 * 
 * 输出: ["1->2->5", "1->3"]
 * 
 * 解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3
 * 
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        class MyNode {
            TreeNode treeNode;
            String path;
            MyNode() {}
            MyNode(TreeNode node) {
                this.treeNode = node;
                this.path = String.valueOf(node.val);
            }
            MyNode createNext(TreeNode node) {
                MyNode next = new MyNode();
                next.treeNode = node;
                next.path = this.path + "->" + String.valueOf(node.val);
                return next;
            }
        }
        List<String> answer = new LinkedList<>();
        if (root == null) {
            return answer;
        }
        LinkedList<MyNode> queue = new LinkedList<>();
        queue.add(new MyNode(root));
        while (!queue.isEmpty()) {
            MyNode cur = queue.pop();
            TreeNode treeNode = cur.treeNode;
            if (treeNode.left == null && treeNode.right == null) {
                answer.add(cur.path);
                continue;
            }
            if (treeNode.left != null) {
                queue.add(cur.createNext(treeNode.left));
            }
            if (treeNode.right != null) {
                queue.add(cur.createNext(treeNode.right));
            }
        }
        return answer;
    }
}

